﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;


namespace ALDIOrganizer
{
    /* This static class is used by my classes which support ICloneable interface.
     * It uses serialization (and generics) in order to create a deep Copy of any given class instance. */
    public static class Cloner
    {
        private static BinaryFormatter _binary;

        public static T Copy<T>(T input)
        {
            if (input == null)
            {
                // System.Diagnostics.Debugger.Break();
                return default(T);
            }
            using (var memory = new MemoryStream())
            {
                _binary = new BinaryFormatter();
                _binary.Serialize(memory, input);
                memory.Seek(0, SeekOrigin.Begin);
                return (T)_binary.Deserialize(memory);
            }
        }

    }
}
